<template>
  <div class="class-list">
    <div class="class-item" v-for="(item, index) in classList" :key="index">
      <div class="name" @click="openSub(item)">
        {{ item.name }}
        <icon
          v-show="item.sub && item.sub.length"
          name="iconxiangyoubeifen16"
          size="14"
        />
      </div>
      <div v-if="item.sub && item.sub.length && item.open" class="sub">
        <div
          class="name"
          v-for="(subitem, subindex) in item.sub"
          :key="subindex"
        >
          {{ subitem.name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ClassList1",
  data() {
    return {
      classList: [
        {
          name: "手用工具",
          sub: [
            { name: "卫浴工具" },
            { name: "电动工具" },
            { name: "水暖器材" },
            { name: "电线电器" },
          ],
        },
        {
          name: "卫浴工具",
          sub: [
            { name: "卫浴工具" },
            { name: "电动工具" },
            { name: "水暖器材" },
            { name: "电线电器" },
          ],
        },
        {
          name: "电动工具",
          sub: [
            { name: "卫浴工具" },
            { name: "电动工具" },
            { name: "水暖器材" },
            { name: "电线电器" },
          ],
        },
        { name: "机械类" },
      ],
    };
  },
  methods: {
    openSub(item) {
      if (item.sub && item.sub.length) {
        item.open = !item.open;
        this.$forceUpdate();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.class-list {
  background: #ffffff;
  border-radius: 0px 0px 5px 5px;
  border: 1px solid #f0f0f0;
  margin-bottom: 20px;
  .class-item {
    .name {
      height: 36px;
      background: #ffffff;
      border: 1px solid #f0f0f0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 12px;
      font-size: 14px;
      font-weight: 400;
      color: #666666;
      line-height: 15px;
    }
    .sub {
      .name {
        border: none;
        padding: 0 12px 0 32px;
      }
    }
  }
}
</style>
